package com.itheima.homework.Two;


import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Demo {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        Student[] s1 = new Student[4];
//        s1[0] = new Student("张三",18);
//        s1[1] = new Student("李四",19);
//        s1[2] = new Student("王五",20);
//        s1[3] = new Student("老六",21);

        for (int i = 0; i < 4; i++) {
            System.out.println("第"+(i+1)+"个对象赋值");
            Student stu = new Student();
            System.out.println("请输入姓名:");
            String a = scanner.next();
            stu.setName(a);
            System.out.println("请输入年龄:");
            int b = scanner.nextInt();
            stu.setAge(b);
            s1[i] = stu;
        }

        Arrays.sort(s1, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int age1 = o1.getAge();
                int age2 = o2.getAge();

                int result = age1 - age2;

                return result;
            }
        });

        for (int i = 0; i < 4; i++) {
            System.out.println(s1[i]);
        }


    }
}
